package mn.slide.petrovis;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.graphics.Typeface;

import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;

import android.widget.RadioButton;
import android.widget.RelativeLayout;

import android.widget.TextView;

public class HomeActivity extends Activity
{
	private boolean						isShow			= true;
	private Button						hideButton;
	private RelativeLayout				animRelLay , textsPlan;
	private RelativeLayout				animRL;
	private Animation					animShow , animHide , switchText;
	private RelativeLayout.LayoutParams	saveLayout;
	private int							saveTopMarign	= 0;
	private RadioButton					button_A80 , button_A95 , button_A92 ,
			button_A98 , button_DIESEL , button_GAS;
	private TextView					gas_name_text , date_text , cost_text;
	private EditText					costMeasure , litrMeasure;
	private boolean						eventRun		= true;

	@Override
	public void onCreate( Bundle savedInstanceState )
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_activity);
		Typeface font = Typeface.createFromAsset(getAssets(), "digifacewideregular.ttf");

		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");
		String formatDate = df.format(c.getTime());

		hideButton = (Button) findViewById(R.id.locator_btn);
		button_A80 = (RadioButton) findViewById(R.id.a80radio_btn);
		button_A92 = (RadioButton) findViewById(R.id.a92radio_btn);
		button_A98 = (RadioButton) findViewById(R.id.a98radio_btn);
		button_A95 = (RadioButton) findViewById(R.id.a95radio_btn);
		button_DIESEL = (RadioButton) findViewById(R.id.dieselradio_btn);
		button_GAS = (RadioButton) findViewById(R.id.gasradio_btn);
		litrMeasure = (EditText) findViewById(R.id.litr_edit);
		costMeasure = (EditText) findViewById(R.id.togrog_edit);
		gas_name_text = (TextView) findViewById(R.id.gas_name_txt);
		date_text = (TextView) findViewById(R.id.date_txt);
		cost_text = (TextView) findViewById(R.id.cost_txt);
		textsPlan = (RelativeLayout) findViewById(R.id.RelativeLayout2);
		animRelLay = (RelativeLayout) findViewById(R.id.mil);
		animRL = (RelativeLayout) findViewById(R.id.animRL);
		animShow = AnimationUtils.loadAnimation(this, R.anim.popup_show);
		animHide = AnimationUtils.loadAnimation(this, R.anim.popup_hide);
		switchText = AnimationUtils.loadAnimation(this, R.anim.scale);
		saveLayout = (RelativeLayout.LayoutParams) animRL.getLayoutParams();

		saveTopMarign = saveLayout.topMargin;
		date_text.setText(formatDate);
		cost_text.setTypeface(font);
		eventsDisable();
		costMeasure.setOnFocusChangeListener(new View.OnFocusChangeListener()
		{
			@Override
			public void onFocusChange( View v , boolean hasFocus )
			{
				// costMeasure.setBackgroundColor(R.drawable.backgroud);
				// litrMeasure.setBackgroundColor(R.color.black);
			}
		});
		litrMeasure.setOnFocusChangeListener(new View.OnFocusChangeListener()
		{
			@Override
			public void onFocusChange( View v , boolean hasFocus )
			{
				// costMeasure.setBackgroundColor(R.color.black);
				// litrMeasure.setBackgroundColor(R.drawable.backgroud);
			}
		});

		textsPlan.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick( View arg0 )
			{
				if (isShow)
					threadDownAnim();
				else
					threadUpAnim();
			}
		});
		button_A80.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick( View arg0 )
			{
				gas_name_text.startAnimation(switchText);
				gas_name_text.setText("A 80");
			}
		});

		button_A92.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick( View arg0 )
			{
				gas_name_text.startAnimation(switchText);
				gas_name_text.setText("A 92");
			}
		});
		button_A95.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick( View arg0 )
			{
				gas_name_text.startAnimation(switchText);
				gas_name_text.setText("A 95");
			}
		});
		button_A98.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick( View arg0 )
			{
				gas_name_text.startAnimation(switchText);
				gas_name_text.setText("A 98");
			}
		});

		button_DIESEL.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick( View arg0 )
			{
				gas_name_text.startAnimation(switchText);
				gas_name_text.setText("DIESEL");
			}
		});
		button_GAS.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick( View arg0 )
			{
				gas_name_text.startAnimation(switchText);
				gas_name_text.setText("GAS");
			}
		});

		hideButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick( View arg0 )
			{
				threadDownAnim();

				HomeTabGroupActivity.group.goLabMedListActivity("1", "main");
			}
		});

		animRL.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick( View v )
			{
				System.out.println("CLICK ");
				if (isShow)
				{
					threadDownAnim();
				}
				else
				{
					threadUpAnim();
				}
			}
		});

	}

	private void threadDownAnim()
	{
		if (eventRun)
		{
			eventRun = false;
			isShow = false;
			eventsEnable();
			animRelLay.startAnimation(animHide);
			animHide.setFillAfter(true);
			hideButton.setEnabled(false);
			final Handler transThreadHandler = new Handler();
			new Thread()
			{
				public void run()
				{
					SystemClock.sleep(1000);
					transThreadHandler.post(new Runnable()
					{
						public void run()
						{
							eventRun = true;
							saveLayout.topMargin = 220;
							animRL.setLayoutParams(saveLayout);
						}
					});
				}
			}.start();
		}
	}

	private void threadUpAnim()
	{
		if (eventRun)
		{
			eventRun = false;
			isShow = true;
			saveLayout.topMargin = saveTopMarign;
			animRL.setLayoutParams(saveLayout);
			animRelLay.startAnimation(animShow);
			animHide.setFillAfter(true);
			hideButton.setEnabled(true);
			final Handler transThreadHandler = new Handler();
			new Thread()
			{
				public void run()
				{
					SystemClock.sleep(1000);
					transThreadHandler.post(new Runnable()
					{
						public void run()
						{
							eventRun = true;
						}
					});
				}
			}.start();
			eventsDisable();
		}
	}

	private void eventsEnable()
	{
		button_A80.setEnabled(true);
		button_A95.setEnabled(true);
		button_A92.setEnabled(true);
		button_A98.setEnabled(true);
		button_DIESEL.setEnabled(true);
		button_GAS.setEnabled(true);
	}

	private void eventsDisable()
	{
		button_A80.setEnabled(false);
		button_A95.setEnabled(false);
		button_A92.setEnabled(false);
		button_A98.setEnabled(false);
		button_DIESEL.setEnabled(false);
		button_GAS.setEnabled(false);
	}
}
